﻿namespace QuickMail.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Media.Imaging;

    using DbWorker.DataModels;

    using QuickMail.DataModels;

    public class LetterVm
    {
        public string Subject { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public DateTime LetterDate { get; set; }
        public LetterType Type { get; set; }

        /// <summary>
        /// Image of rule.
        /// </summary>
        public BitmapImage StatusImage
        {
            get
            {
                return GetStatusIcon(Type);
            }
        }

        public long Id;
        public long RuleId;
        public string Uid;
        public string MessageText;
        public List<FileVm> FileVms = new List<FileVm>();

        //private LetterType _letterType;
        //private BitmapImage _typeImage = StaticFields.NoneImage;
        
        public LetterVm()
        {
        }

        public LetterVm(LetterDm letterDm, IEnumerable<FileDm> fileDms)
        {
            Subject = letterDm.Subject;
            From = letterDm.From;
            To = letterDm.To;
            LetterDate = letterDm.LetterDate;
            Type = letterDm.Type;

            Id = letterDm.Id;
            RuleId = letterDm.RuleId;
            Uid = letterDm.Uid;
            MessageText = letterDm.MessageText;

            foreach (var fileDm in fileDms)
            {
                FileVms.Add(new FileVm(fileDm));
            }
        }

        public LetterDm CreateDataModel()
        {
            return new LetterDm
            {
                // ReSharper disable RedundantThisQualifier
                Subject = this.Subject,
                From = this.From,
                To = this.To,
                LetterDate = this.LetterDate,
                Type = this.Type,
                Id = this.Id,
                RuleId = this.RuleId,
                Uid = this.Uid,
                MessageText = this.MessageText
                // ReSharper restore RedundantThisQualifier
            };
        }

        /// <summary>
        /// Sets the status icon
        /// </summary>
        private BitmapImage GetStatusIcon(LetterType type)
        {
            switch (type)
            {
                case LetterType.Outgoing:
                case LetterType.Incoming:
                    return StaticFields.OkImage;
                case LetterType.NotSent:
                    return StaticFields.ErrorImage;
            }

            return StaticFields.NoneImage;
        }
    }
}
